<?php

function getLocationById($BDD_connexion,$id)
{
	$array = array();
	
	$request_count = "SELECT *, personne.nom AS name FROM location, personne, bien, adresse WHERE location.id_location = :id AND location.ref_personne = personne.id AND location.ref_bien = bien.id_bien AND bien.ref_adresse = adresse.id_adresse";
	$prepare=$BDD_connexion->prepare($request_count);
	$prepare->bindValue(':id',$id,PDO::PARAM_INT);
	$prepare->execute();
	$location = $prepare->fetch(PDO::FETCH_OBJ);
	
	$array[] = $location;
	
	return $array;

}

function getLocationByInt($BDD_connexion)
{
	
	define("SHORT_TIME",60*60*24*31);
	//On ne récupère les locations que dans un délai d'un mois (31 jours)
	
	$array = array();
	
	$request = "SELECT DISTINCT *, personne.nom AS name FROM location, personne, bien, adresse WHERE location.ref_bien = bien.id_bien AND bien.ref_intermediaire = :id_int AND location.etat = 2 AND personne.id = location.ref_personne AND (DATEDIFF(s, '19700101', location.date_debut) - :timestamp) > :shortTime GROUP BY location.id_location";
	$prepare = $BDD_connexion->prepare($request);
	$prepare->bindValue(':id_int',$_SESSION['userid'],PDO::PARAM_INT);
	$prepare->bindValue(':timestamp',mktime(),PDO::PARAM_INT);
	$prepare->bindValue(':shortTime',SHORT_TIME,PDO::PARAM_INT);
	$prepare->execute();
	while ($location = $prepare->fetch(PDO::FETCH_OBJ))
	{
		$array[] = $location;
	}
	
	return $array;
	
}

/*function getClientFromId($BDD_connexion,$id)
{
	$request_count = "SELECT *, personne.nom AS name FROM location, personne WHERE location.id_location = '".$id."' AND location.ref_personne = personne.id";
	$prepare=$BDD_connexion->prepare($request_count);
	$prepare->execute();
	$client = $prepare->fetch(PDO::FETCH_OBJ);
	
	echo $client->name;
	
	return $client;
}*/

?>
